package com.ciwong.epaper.modules.scan.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 宽度尽可能大，高度根据image比例算出
 * Created by res-zwyan on 2015/8/8.
 */
public class MyImageView extends ImageView
{
    public MyImageView(Context context)
    {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    void init(AttributeSet attrs)
    {
//        TypedArray a = getContext().obtainStyledAttributes(attrs,
//                R.styleable.my_imageview);
//        a.getDimensionPixelSize(R.styleable.my_imageview_max_width,10);
//        lineClickBackground = a
//                .getDrawable(R.styleable.speech_text_line_click_background);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        Drawable drawable = getDrawable();
        if (drawable != null)
        {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int diw = drawable.getIntrinsicWidth();
            if (diw > 0)
            {
                int height = width * drawable.getIntrinsicHeight() / diw;
                setMeasuredDimension(width, height);
            }
            else
            {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
